<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      header {
        width: 100%;
        height: 200px;
        background-color: green;
      }
      nav {
        width: 100%;
        height: 100px;
        background-color: yellow;
      }
      main {
        width: 100%;
        height: 2000px;
        background-color: #ccc;
      }
    </style>
  </head>
  <body>
    <header>头部</header>
    <nav id="nav">导航</nav>
    <main>内容</main>
    <script>
      var nav = document.getElementById("nav");
      onscroll = function () {
        var scrollTop =
          document.documentElement.scrollTop || document.body.scrollTop;
        if (scrollTop >= 200) {
          nav.style.position = "fixed";
          nav.style.top = 0;
        } else {
          nav.style.position = "";
        }
      };
    </script>
  </body>
</html>
